using MicroMsg.Common.Utils;
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Resources;
namespace MicroMsg.Storage
{
	public class StorageXml
	{
		public static bool saveToFile<T>(T dataInfo, string xmlFilePath) where T : class
		{
			if (dataInfo == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(xmlFilePath))
			{
				return false;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(xmlFilePath, 2, userStoreForApplication))
					{
						DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));
						dataContractSerializer.WriteObject(isolatedStorageFileStream, dataInfo);
						return true;
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "StorageXml save objcet fail " + ex);
			}
			return false;
		}
		public static T loadFromFile<T>(string xmlFilePath) where T : class
		{
			if (string.IsNullOrEmpty(xmlFilePath))
			{
				return default(T);
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					T result;
					if (userStoreForApplication.FileExists(xmlFilePath))
					{
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(xmlFilePath, 3, userStoreForApplication))
						{
							DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));
							result = (dataContractSerializer.ReadObject(isolatedStorageFileStream) as T);
							return result;
						}
					}
					result = default(T);
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "StorageXml read objcet fail " + ex);
			}
			return default(T);
		}
		public static byte[] saveToBuffer<T>(T dataInfo) where T : class
		{
			if (dataInfo == null)
			{
				return null;
			}
			try
			{
				MemoryStream memoryStream = new MemoryStream();
				DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));
				dataContractSerializer.WriteObject(memoryStream, dataInfo);
				return memoryStream.ToArray();
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "StorageXml save MemoryStream fail " + ex);
			}
			return null;
		}
		public static T loadFromBuffer<T>(byte[] xmlBuffer) where T : class
		{
			if (xmlBuffer != null && xmlBuffer.Length > 0)
			{
				try
				{
					MemoryStream memoryStream = new MemoryStream(xmlBuffer);
					DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));
					return dataContractSerializer.ReadObject(memoryStream) as T;
				}
				catch (Exception ex)
				{
					Log.smethod_1("storage", "StorageXml read objcet fail " + ex);
				}
				return default(T);
			}
			return default(T);
		}
		public static T loadFromStream<T>(Stream xmlStram) where T : class
		{
			if (xmlStram == null)
			{
				return default(T);
			}
			try
			{
				DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));
				return dataContractSerializer.ReadObject(xmlStram) as T;
			}
			catch (Exception ex)
			{
				Log.smethod_1("storage", "StorageXml read stream fail " + ex);
			}
			return default(T);
		}
		public static bool saveObject<T>(T item, string xmlFilePath = null) where T : class
		{
			XmlSeriData<T> dataInfo = new XmlSeriData<T>(item);
			if (xmlFilePath == null)
			{
				xmlFilePath = StorageIO.getRooDir() + "/" + typeof(T).get_Name() + ".xml";
			}
			return StorageXml.saveToFile<XmlSeriData<T>>(dataInfo, xmlFilePath);
		}
		public static T loadObject<T>(string xmlFilePath = null) where T : class
		{
			if (xmlFilePath == null)
			{
				xmlFilePath = StorageIO.getRooDir() + "/" + typeof(T).get_Name() + ".xml";
			}
			XmlSeriData<T> xmlSeriData = StorageXml.loadFromFile<XmlSeriData<T>>(xmlFilePath);
			if (xmlSeriData != null)
			{
				return xmlSeriData.data;
			}
			return default(T);
		}
		public static T loadResObject<T>(Uri resUri) where T : class
		{
			StreamResourceInfo resourceStream = Application.GetResourceStream(resUri);
			if (resourceStream == null)
			{
				return default(T);
			}
			XmlSeriData<T> xmlSeriData = StorageXml.loadFromStream<XmlSeriData<T>>(resourceStream.get_Stream());
			if (xmlSeriData != null)
			{
				return xmlSeriData.data;
			}
			return default(T);
		}
		public static void deleteFile<T>(string xmlFilePath = null) where T : class
		{
			if (xmlFilePath == null)
			{
				xmlFilePath = StorageIO.getRooDir() + "/" + typeof(T).get_Name() + ".xml";
			}
			StorageIO.deleteFile(xmlFilePath);
		}
		private static string FilterString(string string_0)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < string_0.get_Length(); i++)
			{
				char c = string_0.get_Chars(i);
				int num = (int)c;
				if ((num >= 0 && num <= 8) || (num >= 11 && num <= 12) || (num >= 14 && num <= 31))
				{
					stringBuilder.Append(' ');
				}
				else
				{
					stringBuilder.Append(c);
				}
			}
			return stringBuilder.ToString();
		}
		public static void FilterInvalidCharacters(object object_0)
		{
			try
			{
				FieldInfo[] fields = object_0.GetType().GetFields(20);
				FieldInfo[] array = fields;
				for (int i = 0; i < array.Length; i++)
				{
					FieldInfo fieldInfo = array[i];
					if (fieldInfo.get_MemberType() == 4 && fieldInfo.get_FieldType() == typeof(string))
					{
						string text = fieldInfo.GetValue(object_0) as string;
						if (!string.IsNullOrEmpty(text))
						{
							string text2 = StorageXml.FilterString(text);
							if (!(text2 == text))
							{
								fieldInfo.SetValue(object_0, text2);
							}
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}
		public static void test()
		{
			testXmlData testXmlData = StorageXml.loadFromFile<testXmlData>("test.xml");
			if (testXmlData == null)
			{
				testXmlData dataInfo = new testXmlData
				{
					int_0 = 111,
					int_1 = 222
				};
				StorageXml.saveToFile<testXmlData>(dataInfo, "test.xml");
				return;
			}
			testXmlData.int_0 = 333;
		}
	}
}
